<?php

namespace app\validate;

use app\model\CategoryModel;
use think\Validate;

class CategoryValidate extends Validate
{
    protected $rule = [
        'id' => 'require',
        'name' => 'require',
        'type' => 'require|checkType'
    ];

    protected $message = [
        'name.require' => '请输入分类名称',
        'type.checkType' => '请选择正确的分类类型',
        'id.require' => '请选择分类'
    ];

    protected $scene = [
        'add' => ['name', 'type'],
        'edit' => ['id', 'name'],
        'del' => ['id'],
        'treeList' => ['type'],
    ];

    public function checkType($value)
    {
        if (!in_array($value, CategoryModel::TYPE_ARRAY)) {
            return '请选择正确的分类类型';
        }

        return true;
    }
}